﻿using NHibernate;
using NHibernate.Cfg;
using SmartRS.com.vn.smartrs.config;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Xml;
using System.Xml.Linq;

namespace SmartRS.com.vn.smartrs.form.config
{
    public partial class frmConfig : Form
    {
        private string logPath = "";
        private string dbServer = "";
        private string dbName = "";
        private string dbUser = "";
        private string dbPassword = "";
        private string restaurantName = "";
        private string restaurentAddress = "";
        private string restaurantPhone = "";

        public delegate void SetConfigValue(string _logPath, string _dbServer, string _dbName, string _dbUser,
            string _dbPassword, string _restaurantName, string _restaurantAddress, string _restaurantPhone);
        public SetConfigValue setConfigValue;

        public delegate void GetResult(bool result);
        public event GetResult getResult;
        public frmConfig()
        {
            InitializeComponent();

            setConfigValue = new SetConfigValue(setConfig);
            /*try
            {
                //var configuration = new Configuration();
                //configuration.Configure();
                //configuration.SetProperty("connection.connection_string", String.Format("Server={0};Database={1};User ID={2};Password={3}", "localhost", "SmartRS1", "postgres", "123456"));
                //ISessionFactory sf = configuration.BuildSessionFactory();

                XmlDocument xmlDoc = new XmlDocument();

                //xmlDoc.Load("E:\\Project\\SVN\\SmartRS\\02.DD\\SmartRS\\SmartRS\\hibernate.cfg.xml");
                xmlDoc.Load("hibernate.cfg.xml");

                XmlNamespaceManager nm = new XmlNamespaceManager(xmlDoc.NameTable);

                nm.AddNamespace("HC", "urn:nhibernate-configuration-2.2");

                XmlNode node = xmlDoc.SelectSingleNode("//HC:session-factory/HC:property[@name='connection.connection_string']", nm);

                node.InnerText = "\r\n      Server=localhost;Database=SmartRS;User ID=postgres;Password=123s456;\r\n    ";

                //xmlDoc.Save("E:\\Project\\SVN\\SmartRS\\02.DD\\SmartRS\\SmartRS\\hibernate.cfg.xml");
                xmlDoc.Save("hibernate.cfg.xml");

                //ISessionFactory sf = new Configuration().Configure().BuildSessionFactory();



            }
            catch(Exception ex)
            {
                MessageBox.Show(ex.Message);
            }*/
        }

        private void setConfig(string _logPath, string _dbServer, string _dbName, string _dbUser,
            string _dbPassword, string _restaurantName, string _restaurantAddress, string _restaurantPhone)
        {
            logPath = _logPath;
            dbServer = _dbServer;
            dbName = _dbName;
            dbUser = _dbUser;
            dbPassword = _dbPassword;
            restaurantName = _restaurantName;
            restaurentAddress = _restaurantAddress;
            restaurantPhone = _restaurantPhone;
        }
        private void frmConfig_Load(object sender, EventArgs e)
        {
            //Validate value
            validateValue();
        }

        private void btnBrowse_Click(object sender, EventArgs e)
        {
            fbdLogPath.SelectedPath = Thread.GetDomain().BaseDirectory;
            DialogResult result = this.fbdLogPath.ShowDialog();
            
            if(result == DialogResult.OK)
            {
                txtLogPath.Text = fbdLogPath.SelectedPath;
            }
        }

        private void btnHuy_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void btnHoanTat_Click(object sender, EventArgs e)
        {
            /*logPath = txtLogPath.Text.Trim();
            dbServer = txtServerName.Text.Trim();
            dbName = txtDbName.Text.Trim();
            dbUser = txtUserName.Text.Trim();
            dbPassword = txtPassword.Text.Trim();
            restaurantName = txtResturantName.Text.Trim();
            restaurentAddress = txtAddress.Text.Trim();
            restaurantPhone = txtPhone.Text.Trim();

            //validate value
            if(!validateValue())
            {
                //Check db connection
                if(!InitConfig.checkDbConnection(dbServer, dbName, dbUser, dbPassword))
                {
                    return;
                }
                
                //Write config to hibernate.cfg.xml
                if(!InitConfig.writeConfigDb(dbServer, dbName, dbUser, dbPassword))
                {
                    return;
                }

                if(!InitConfig.writeAppconfig("logPath", logPath, "restaurantName", restaurantName,
                    "restaurentAddress", restaurentAddress, "restaurantPhone", restaurantPhone))
                {
                    return;
                }
                if(getResult!= null)
                {
                    getResult(true);
                    this.Close();
                }
                else
                {
                    this.Close();
                }
            }*/
        }

        private bool validateValue()
        {
            bool hasError = false;
            epConfig.Clear();
            if (logPath.Equals(""))
            {
                hasError = true;
                epConfig.SetError(this.txtLogPath, "Giá trị không hợp lệ!");
            }
            else
            {
                txtLogPath.Text = logPath;
            }
            if (dbServer.Equals(""))
            {
                hasError = true;
                epConfig.SetError(this.txtServerName, "Giá trị không hợp lệ!");
            }
            else
            {
                txtServerName.Text = dbServer;
            }
            if (dbName.Equals(""))
            {
                hasError = true;
                epConfig.SetError(this.txtDbName, "Giá trị không hợp lệ!");
            }
            else
            {
                txtDbName.Text = dbName;
            }
            if (dbUser.Equals(""))
            {
                hasError = true;
                epConfig.SetError(this.txtUserName, "Giá trị không hợp lệ!");
            }
            else
            {
                txtUserName.Text = dbUser;
            }
            if (dbPassword.Equals(""))
            {
                hasError = true;
                epConfig.SetError(this.txtPassword, "Giá trị không hợp lệ!");
            }
            else
            {
                txtPassword.Text = dbPassword;
            }
            if (restaurantName.Equals(""))
            {
                hasError = true;
                epConfig.SetError(this.txtResturantName, "Giá trị không hợp lệ!");
            }
            else
            {
                txtResturantName.Text = restaurantName;
            }
            if (restaurentAddress.Equals(""))
            {
                hasError = true;
                epConfig.SetError(this.txtAddress, "Giá trị không hợp lệ!");
            }
            else
            {
                txtAddress.Text = restaurentAddress;
            }
            if (restaurantPhone.Equals(""))
            {
                hasError = true;
                epConfig.SetError(this.txtPhone, "Giá trị không hợp lệ!");
            }
            else
            {
                txtPhone.Text = restaurantPhone;
            }
            return hasError;
        }
    }
}
